package cn.chuanw.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.chuanw.wx.annotation.RequiresPermissionsDesc;
import cn.chuanw.core.util.RandomNumberUtil;
import cn.chuanw.core.util.ResponseUtil;
import cn.chuanw.db.domain.WRoomreturnRecord;
import cn.chuanw.db.service.RoomreturnRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: wangkun
 * @Date:  20-02-09 下午 4:06
 * @Description: 租户退房记录
 **/
@RestController
@RequestMapping("/admin/roomreturnRecord")
@Validated
public class WRoomreturnRecordController {
    private final Log logger = LogFactory.getLog(WRoomreturnRecordController.class);

    @Autowired
    private RoomreturnRecordService roomreturnRecordService;

    @RequiresPermissions("admin:roomreturnRecord:list")
    @RequiresPermissionsDesc(menu = {"公寓管理", "退租记录"}, button = "查询")
    @GetMapping("/list")
    public Object list(String tenantId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        List<WRoomreturnRecord> WRoomreturnRecords = roomreturnRecordService.querySelective(tenantId, page, limit);
        return ResponseUtil.okList(WRoomreturnRecords);
    }


    @RequiresPermissions("admaddin:roomreturnRecord:create")
    @RequiresPermissionsDesc(menu = {"公寓管理", "退租记录"}, button = "新增")
    @PostMapping("/create")
    public Object add(@RequestBody WRoomreturnRecord wRoomreturnRecord) {
        wRoomreturnRecord.setReturnTime(LocalDateTime.now());
        wRoomreturnRecord.setId(RandomNumberUtil.getStringRandom(12));
        roomreturnRecordService.create(wRoomreturnRecord);
        return ResponseUtil.ok(wRoomreturnRecord);
    }


    @RequiresPermissions("admaddin:roomreturnRecord:update")
    @RequiresPermissionsDesc(menu = {"公寓管理", "退租记录"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody WRoomreturnRecord wRoomreturnRecord) {
        wRoomreturnRecord.setReturnTime(LocalDateTime.now());
        roomreturnRecordService.updateById(wRoomreturnRecord);
        return ResponseUtil.ok(wRoomreturnRecord);
    }
}
